// leaderPerson/pages/groupDragon/service/service.js
var leader = require("../../../../common/leader.js").leader;

const {common: common} = require("../../../../common/common.js");

var app = getApp();

Page({
    /**
   * 页面的初始数据
   */
    data: {
        pro_name: "",
        page: 1,
        goodsList: [],
        room_id: "",
        selectIds: [],
        selectId: "",
        sorts: [ {
            fieldName: "created_at",
            ifAsc: false
        } ],
        business_id: "",
        ifSelf: false,
        screenItems: [ {
            key: 0,
            name: "发布时间",
            active: true,
            fieldName: "created_at",
            ifAsc: false
        }, {
            key: 1,
            name: "商品单价",
            active: false,
            fieldName: "sale_price",
            ifAsc: false
        }, {
            key: 2,
            name: "佣金",
            active: false,
            fieldName: "brokerage_price",
            ifAsc: false
        } ],
        comeon: 1,
        //2:直播购
        bId: ""
    },
    /**
   * 生命周期函数--监听页面加载
   */
    onLoad(options) {
        let selectIdss = options.selectIds ? JSON.parse(options.selectIds) : [];
        let selectIds = [];
        selectIdss.forEach(item => {
            selectIds.push(item.toString());
        });
        this.setData({
            selectId: selectIds.length ? selectIds[0] : "",
            selectIds: selectIds.length ? selectIds : [],
            //传入的selectIds
            selectIds_pre: selectIds.length ? selectIds : [],
            //传入的selectIds
            room_id: options.room_id || "",
            comeon: options.comeon || 1
        });
    },
    /**
   * 生命周期函数--监听页面初次渲染完成
   */
    onReady() {},
    /**
   * 生命周期函数--监听页面显示
   */
    onShow() {
        if (app.globalData.user) {
            //如果直接拿到openid
            this.requestStart(app.globalData.user.openid);
        } else {
            //回调拿到openid
            app.userInfoReadyCallback = (res => {
                this.requestStart(res.openid);
            });
        }
    },
    /**
   * 生命周期函数--监听页面隐藏
   */
    onHide() {},
    /**
   * 生命周期函数--监听页面卸载
   */
    onUnload() {},
    /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
    onPullDownRefresh() {},
    /**
   * 页面上拉触底事件的处理函数
   */
    onReachBottom() {
        this.getData();
    },
    /**
   * 用户点击右上角分享
   */
    onShareAppMessage() {},
    requestStart: function(openid) {
        leader.getMyLeaderInfo_aync(openid, "").then(() => {
            this.getData();
            this.setData({
                business_id: this.data.myleaderinfo.leader_shop.shop_id
            });
        });
    },
    getData() {
        var that = this;
        common.request({
            endpoint: "Localbusiness/product/product-list",
            method: "post",
            data: {
                // business_id: that.data.myleaderinfo.leader_shop.shop_id,
                business_id: that.data.ifSelf === true ? that.data.myleaderinfo.leader_shop.shop_id : 0,
                page: that.data.page,
                page_size: 10,
                name: that.data.pro_name,
                sorts: that.data.sorts
            },
            success: res => {
                let result = res.extra;
                let arr = that.data.page == 1 ? [] : that.data.goodsList;
                arr.push(...result);
                this.setData({
                    showLoading: false,
                    goodsList: arr,
                    page: that.data.page + 1
                });
            }
        });
    },
    /**
   * 记录input值
   * @param {*} e 
   */
    recordValue: function(e) {
        this.setData({
            pro_name: e.detail.value
        });
        if (!this.data.pro_name) {
            this.search();
        }
    },
    search() {
        let that = this;
        that.setData({
            page: 1,
            goodsList: []
        });
        that.getData();
    },
    confirmSelect(e) {
        if (e.currentTarget.dataset.stock <= 0) {
            return false;
        }
        let selectIds = [ e.currentTarget.dataset.id ];
        if (this.data.room_id != "") {
            this.setData({
                selectIds: selectIds,
                bId: e.currentTarget.dataset.bid
            });
            this.importGoods();
        } else {
            const pages = getCurrentPages();
            const prevPage = pages[pages.length - 2];
            if (prevPage) {
                prevPage.setData({
                    selectIds: selectIds,
                    selectIds_pre: selectIds
                });
                wx.navigateBack();
            }
        }
    },
    //导入商品
    importGoods: common.throttle(function(e) {
        let that = this;
        let arr = [];
        that.data.selectIds.forEach(item => {
            let data = {
                product_id: item,
                source: 3
            };
            arr.push(data);
        });
        wx.showLoading({
            title: "正在导入"
        });
        common.request({
            endpoint: "LiveShopping/manage/product-import",
            method: "post",
            data: {
                room_id: that.data.room_id,
                products: arr
            },
            success: res => {
                wx.showToast({
                    title: "导入成功"
                });
                const pages = getCurrentPages();
                const prevPage = pages[pages.length - 2];
                prevPage.getData();
                wx.navigateBack();
            },
            fail: err => {
                wx.showModal({
                    title: "提示",
                    content: err.message
                });
            },
            complete: err => {
                wx.hideLoading();
            }
        });
    }, 3e3),
    clickScreen(e) {
        let items = this.data.screenItems;
        items.map((item, index) => {
            if (item.key == e.currentTarget.dataset.key) {
                if (item.active === true) {
                    if (item.ifAsc === true) {
                        item.ifAsc = false;
                    } else {
                        item.ifAsc = true;
                    }
                } else {
                    item.active = true;
                    item.ifAsc = false;
                }
            } else {
                item.active = false;
                item.ifAsc = false;
            }
        });
        let obj = {
            fieldName: items[e.currentTarget.dataset.key]["fieldName"],
            ifAsc: items[e.currentTarget.dataset.key]["ifAsc"]
        };
        this.setData({
            screenItems: items,
            sorts: [ obj ]
        });
        this.search();
        console.log(this.data.screenItems[e.currentTarget.dataset.key]);
    },
    clickFilter() {
        if (this.data.ifSelf === false) {
            this.setData({
                ifSelf: true
            });
            this.search();
        } else {
            this.setData({
                ifSelf: false
            });
            this.search();
        }
    }
});